package com.timmy.video.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.timmy.video.R;
import com.timmy.video.ui.activity.MainActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: Timmy
 * @Data: 2017/6/14
 * @Fuction:
 */

public class VideoAdapter extends RecyclerView.Adapter {

    private List<MainActivity.VideoInfo> mVideoInfos;
    private Context mContext;

    public VideoAdapter(Context context) {
        mVideoInfos = new ArrayList<>();
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(mContext).inflate(R.layout.item_blelist, parent, false);
        ItemViewHolder holder = new ItemViewHolder(itemView);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ItemViewHolder) holder).setData(mVideoInfos.get(position));
    }

    @Override
    public int getItemCount() {
        return mVideoInfos.size();
    }

    public void addData(List<MainActivity.VideoInfo> videoInfos) {
        mVideoInfos = videoInfos;
        notifyDataSetChanged();
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {

        private TextView mTextView;
        private MainActivity.VideoInfo mData;

        public ItemViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.item_text);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((MainActivity) mContext).onClickItem(mData);
                }
            });
        }

        public void setData(MainActivity.VideoInfo data) {
            mData = data;
            mTextView.setText(data.title);
        }
    }

}
